package com.geekhalo.lego.plugin.preoperation;

import com.geekhalo.lego.plugin.support.template.CreateClassContext;
import com.geekhalo.lego.plugin.support.util.Utils;

public class PreOperationTemplate {

    private static final String TEMPLATE =
                    "package {pkg};\n" +
                    "\n" +
                    "import com.geekhalo.lego.core.command.support.handler.preaction.SmartPreOperation;\n" +
                    "import org.springframework.stereotype.Service;\n" +
                    "import lombok.extern.slf4j.Slf4j;\n" +
                    "import {cxtTypeFull};\n" +
                    "\n" +
                    "@Service\n" +
                    "@Slf4j\n" +
                    "public class {className}\n" +
                            " implements SmartPreOperation<{cxtType}> {\n" +
                    "\n" +
                    "    @Override\n" +
                    "    public boolean support(Class cls) {\n" +
                    "        return cls == {cxtType}.class;\n" +
                    "    }\n" +
                    "\n" +
                    "    @Override\n" +
                    "    public void action({cxtType} context) {\n" +
                    "       //TODO 增加操作代码\n" +
                    "    }\n" +
                    "}\n";
    public static String create(CreatePreOperationContext context){
        String content = TEMPLATE;
        content = content.replace("{pkg}", context.getPkg());
        content = content.replace("{className}", context.getClsName());
        content = content.replace("{cxtTypeFull}", context.getCxtTypeFull());
        content = content.replace("{cxtType}", context.getCxtType());


        return content;
    }

    public static class CreatePreOperationContext extends CreateClassContext {
        private String cxtTypeFull;
        private String cxtType;
        public CreatePreOperationContext(String pkg, String clsName) {
            super(pkg, clsName);
        }

        public void setCxtTypeFull(String cxtTypeFull) {
            this.cxtTypeFull = cxtTypeFull;
            this.cxtType = Utils.getType(cxtTypeFull);
        }
        public String getCxtTypeFull() {
            return cxtTypeFull;
        }

        public String getCxtType() {
            return cxtType;
        }
    }
}
